package com.hllxd.hism.star.basic.service.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommonUtils {
    public static <T> boolean isNull(T... objects) {
        for (T obj : objects) {
            if (null == obj) {
                return true;
            }
        }
        return false;
    }

    public static <T> boolean notNull(T... objects) {
        for (T obj : objects) {
            if (null == obj) {
                return false;
            }
        }
        return true;
    }

    public static boolean isBlank(String... objects) {
        for (String obj : objects) {
            if (null == obj || obj.isEmpty()) {
                return false;
            }
        }
        return true;
    }

    public static boolean notBlank(String... objects) {
        for (String obj : objects) {
            if (null == obj || obj.isEmpty()) {
                return true;
            }
        }
        return false;
    }

    public static boolean notEmpty(Collection collection) {

        if (isNull(collection)) {
            return false;
        }
        if (collection.size() > 0) {
            return false;
        }

        return true;
    }

    public static boolean isEmpty(Collection collection) {
        if (isNull(collection)) {
            return true;
        }
        if (collection.size() <= 0) {
            return true;
        }
        return false;
    }
    public static boolean isEmpty(String[] collection) {
        if (isNull(collection)) {
            return true;
        }
        if (collection.length <= 0) {
            return true;
        }
        return false;
    }

    /**
     * @param func s如果不为null,则执行此表达式
     * @param nullDefalut 如果s为null直接返回这个值
     */
    public static <T,R> R toNullDefalut(T t,Function<T, R> func, R nullDefalut){
        if(null == t){
            return nullDefalut;
        }
        return func.apply(t);
    }

    public static <T extends Collection> boolean hasTheSameItem(T main, T second) {
        if (CommonUtils.notEmpty(main) && CommonUtils.notEmpty(second)) {
            for (Object objects : main) {
                if (second.contains(main)) {
                    return true;
                }
            }
        }

        return false;
    }

    public static List<String> trimAndSplitBySemicolon(String s) {
        return (ArrayList) Arrays.asList(s.trim().split("\\s*;\\s*"));
    }
    public static String replaceBlank(String str) {
        String dest = "";
        if (str!=null) {
            String pattern = "\\s*|\t|\r|\n";
            Pattern p = Pattern.compile(pattern);
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }

    /**
     * 请求是否成功（响应码是只包含数字，且n个0表示成功）
     * <p><code>null</code> 或者 "" 都返回false</p>
     *
     * @param code 响应码
     * @return boolean 是否成功
     * @author liuxiongbo
     * @date 2020/12/18 23:08
     */
    public static boolean requestIsSuccess(String code) {
        if (StringUtils.isBlank(code) || !NumberUtils.isDigits(code)) {
            return false;
        }
        long codeNum = Long.parseLong(code);
        return codeNum == 0;
    }

    /**
     * 字符根据位数补零
     * @param str
     * @param size
     * @return
     */
    public static<T> String zeroizeString(T t, int size) {
        return String.format("%" + size + "s", t).replaceAll(" ", "0");
    }
}
